﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Client.TopLevel;

namespace PersApp
{
    public partial class ReportForm : Form
    {
        private IManagerEmployee mManager;
        IEnumerable<TLReport> reports;

        public ReportForm(IManagerEmployee manager)
        {
            InitializeComponent();
            mManager = manager;
            initTable();
        }

        private void initTable()
        {            
            employeesGridView.Columns.Add("name", "Имя");
            employeesGridView.Columns.Add("login", "Логин");
            employeesGridView.Columns.Add("password", "Пароль");
            employeesGridView.Columns.Add("position", "Должность");
            employeesGridView.Columns.Add("role", "Полномочия");
            employeesGridView.Columns.Add("bank_account", "Номер счета");
            employeesGridView.Columns.Add("sum_time", "Суммарное рабочее время");
            dataGridView1.Columns.Add("timein", "Время прихода");
            dataGridView1.Columns.Add("timeout", "Время ухода");

            foreach (DataGridViewColumn column in employeesGridView.Columns)
            {
                //column.SortMode = DataGridViewColumnSortMode.NotSortable;
                column.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
            }

            foreach (DataGridViewColumn column in dataGridView1.Columns)
            {
                //column.SortMode = DataGridViewColumnSortMode.NotSortable;
                column.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
            }

            refreshList();
        }

        private void refreshList()
        {
            employeesGridView.Rows.Clear();
            dataGridView1.Rows.Clear();

            if (reports == null) return;

            foreach (TLReport report in reports)
            {
                TLEmployee employee           = report.Employee;
                IEnumerable<TLWorkTime> times = report.Time;
                TimeSpan timeSpan             = new TimeSpan();

                if (times != null)
                {
                    foreach (TLWorkTime time in times)
                    {
                        if (time.TimeOut.Year > 1)
                        {
                            timeSpan += (time.TimeOut - time.TimeIn);
                        }
                    }
                }

                TLPosition position = getPositionByIndex(employee.PositionId);
                Role role           = Roles.getRoleByIndex(employee.Role);

                int rowIndex = employeesGridView.Rows.Add(
                    employee.Name,
                    employee.Login,
                    employee.Password,
                    position,
                    role,
                    employee.BankAccountId,
                    (timeSpan.TotalHours.ToString() + " часов"));

                employeesGridView.Rows[rowIndex].Tag = employee;
            }
        }

        private TLPosition getPositionByIndex(int index)
        {
            TLPosition position = null;
            IEnumerable<TLPosition> positions = mManager.GetAllPositions();
            List<TLPosition> mPositions = new List<TLPosition>(positions);

            foreach (TLPosition p in mPositions)
            {
                if (p.Id == index)
                {
                    position = p;
                    break;
                }
            }

            return position;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                DateTime startTime = startDateTimePicker.Value;
                DateTime finishTime = finishDateTimePicker.Value;

                reports = mManager.GetAllReports(startTime, finishTime);

                refreshList();
            }
            catch { }
        }

        private void employeesGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                dataGridView1.Rows.Clear();

                if (e.RowIndex < 0)
                    return;

                TLEmployee employee = (TLEmployee)employeesGridView.Rows[e.RowIndex].Tag;

                if (reports != null)
                {
                    foreach (TLReport report in reports)
                    {
                        if (report.Employee.Id == employee.Id)
                        {
                            foreach (TLWorkTime time in report.Time)
                            {
                                dataGridView1.Rows.Add(time.TimeIn.AddHours(2.0).ToString(), time.TimeOut.AddHours(2.0).ToString());
                            }

                            break;
                        }
                    }
                }
            }
            catch { }
        }
    }
}
